package cn.wenhe9.novel.manager;

import cn.wenhe9.novel.constant.CacheConsts;
import cn.wenhe9.novel.constant.DatabaseConsts;
import cn.wenhe9.novel.domain.dto.BookCategoryDto;
import cn.wenhe9.novel.domain.entity.book.BookCategory;
import cn.wenhe9.novel.mapper.BookCategoryMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 小说分类 缓存管理类
 *
 * @author DuJinliang
 * 2022/5/18
 */
@Component
public class BookCategoryCacheManager {

    @Resource
    private BookCategoryMapper bookCategoryMapper;

    /**
     * 根据作品方向查询小说分类列表，并放入缓存中
     */
    @Cacheable(cacheManager = CacheConsts.REDIS_CACHE_MANAGER
            , value = CacheConsts.BOOK_CATEGORY_LIST_CACHE_NAME)
    public List<BookCategoryDto> listCategory(Integer workDirection) {
        QueryWrapper<BookCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DatabaseConsts.BookCategoryTable.COLUMN_WORK_DIRECTION, workDirection);

        return bookCategoryMapper.selectList(queryWrapper).stream().map(item ->
                BookCategoryDto.builder()
                        .id(item.getId())
                        .name(item.getName())
                        .build()).collect(Collectors.toList());
    }

}
